<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <!-- <el-form-item label="厂家id" prop="tenantId">
         <el-input
           v-model="queryParams.tenantId"
           placeholder="请输入厂家id"
           clearable
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>-->
      <el-form-item label="结算类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择结算类型" clearable>
          <el-option
            v-for="dict in dict.type.jiesuan_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库代码" prop="warehouseCode">
        <el-input
          v-model="queryParams.warehouseCode"
          placeholder="请输入仓库代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库类型" prop="chukuType">
        <el-select v-model="queryParams.chukuType" placeholder="请选择出库类型" clearable>
          <el-option
            v-for="dict in dict.type.chuku_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库类型" prop="rukuType">
        <el-select v-model="queryParams.rukuType" placeholder="请选择入库类型" clearable>
          <el-option
            v-for="dict in dict.type.ruku_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出库仓库代码" prop="ckCode">
        <el-input
          v-model="queryParams.ckCode"
          placeholder="请输入出库仓库代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库仓库名称" prop="ckName">
        <el-input
          v-model="queryParams.ckName"
          placeholder="请输入出库仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库仓库代码" prop="rkCode">
        <el-input
          v-model="queryParams.rkCode"
          placeholder="请输入入库仓库代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库仓库名称" prop="rkName">
        <el-input
          v-model="queryParams.rkName"
          placeholder="请输入入库仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商代码" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="请输入供应商代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制造商代码" prop="manufacturerCode">
        <el-input
          v-model="queryParams.manufacturerCode"
          placeholder="请输入制造商代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制造商名称" prop="manufacturerName">
        <el-input
          v-model="queryParams.manufacturerName"
          placeholder="请输入制造商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="托管状态" prop="tgState">
        <el-input
          v-model="queryParams.tgState"
          placeholder="请输入托管状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算控制" prop="isJiesuan">
        <el-select v-model="queryParams.isJiesuan" placeholder="请选择结算控制" clearable>
          <el-option
            v-for="dict in dict.type.jiesuan_contro"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="结算周期" prop="jsZq">
         <el-input
           v-model="queryParams.jsZq"
           placeholder="请输入结算周期"
           clearable
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>
       <el-form-item label="月结算日" prop="jsDay">
         <el-input
           v-model="queryParams.jsDay"
           placeholder="请输入月结算日"
           clearable
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>
       <el-form-item label="创建人" prop="createUser">
         <el-input
           v-model="queryParams.createUser"
           placeholder="请输入创建人"
           clearable
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>
       <el-form-item label="修改人" prop="updateUser">
         <el-input
           v-model="queryParams.updateUser"
           placeholder="请输入修改人"
           clearable
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:jsRules:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:jsRules:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:jsRules:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:jsRules:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jsRulesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" />
       <el-table-column label="厂家id" align="center" prop="tenantId" />-->
      <el-table-column label="结算类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jiesuan_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="仓库代码" width="180" align="center" prop="warehouseCode" />
      <el-table-column label="仓库名称" width="180" align="center" prop="warehouseName" />
      <el-table-column label="出库类型" align="center" prop="chukuType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.chuku_type" :value="scope.row.chukuType"/>
        </template>
      </el-table-column>
      <el-table-column label="入库类型" align="center" prop="rukuType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ruku_type" :value="scope.row.rukuType"/>
        </template>
      </el-table-column>
      <el-table-column label="出库仓库代码" width="180" align="center" prop="ckCode" />
      <el-table-column label="出库仓库名称" width="180" align="center" prop="ckName" />
      <el-table-column label="入库仓库代码" width="180" align="center" prop="rkCode" />
      <el-table-column label="入库仓库名称" width="180" align="center" prop="rkName" />
      <el-table-column label="供应商代码" width="180" align="center" prop="supplierCode" />
      <el-table-column label="供应商名称" width="180" align="center" prop="supplierName" />
      <el-table-column label="制造商代码" width="180" align="center" prop="manufacturerCode" />
      <el-table-column label="制造商名称" width="180" align="center" prop="manufacturerName" />
      <el-table-column label="托管状态" width="180" align="center" prop="tgState" />
      <el-table-column label="结算控制" align="center" prop="isJiesuan">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jiesuan_contro" :value="scope.row.isJiesuan"/>
        </template>
      </el-table-column>
      <el-table-column label="结算周期" align="center" prop="jsZq" />
      <el-table-column label="月结算日" align="center" prop="jsDay" />
      <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="修改人" align="center" prop="updateUser" />
      <el-table-column width="100" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:jsRules:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:jsRules:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改结算规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="24">
          <!--
                    <el-col :span="12">
                      <el-form-item label="厂家id" prop="tenantId">
                        <el-input v-model="form.tenantId" placeholder="请输入厂家id" />
                      </el-form-item>
                    </el-col>
          -->
          <el-col :span="24">
            <el-form-item label="结算类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择结算类型">
                <el-option
                  v-for="dict in dict.type.jiesuan_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库代码" prop="warehouseCode">
              <el-input v-model="form.warehouseCode" placeholder="请输入仓库代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="warehouseName">
              <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库类型" prop="chukuType">
              <el-select v-model="form.chukuType" placeholder="请选择出库类型">
                <el-option
                  v-for="dict in dict.type.chuku_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库类型" prop="rukuType">
              <el-select v-model="form.rukuType" placeholder="请选择入库类型">
                <el-option
                  v-for="dict in dict.type.ruku_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库仓库代码" prop="ckCode">
              <el-input v-model="form.ckCode" placeholder="请输入出库仓库代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库仓库名称" prop="ckName">
              <el-input v-model="form.ckName" placeholder="请输入出库仓库名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库仓库代码" prop="rkCode">
              <el-input v-model="form.rkCode" placeholder="请输入入库仓库代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库仓库名称" prop="rkName">
              <el-input v-model="form.rkName" placeholder="请输入入库仓库名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商代码" prop="supplierCode">
              <el-input v-model="form.supplierCode" placeholder="请输入供应商代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制造商代码" prop="manufacturerCode">
              <el-input v-model="form.manufacturerCode" placeholder="请输入制造商代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制造商名称" prop="manufacturerName">
              <el-input v-model="form.manufacturerName" placeholder="请输入制造商名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="托管状态" prop="tgState">
              <el-input v-model="form.tgState" placeholder="请输入托管状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算控制" prop="isJiesuan">
              <el-select v-model="form.isJiesuan" placeholder="请选择结算控制">
                <el-option
                  v-for="dict in dict.type.jiesuan_contro"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算周期" prop="jsZq">
              <el-input v-model="form.jsZq" placeholder="请输入结算周期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="月结算日" prop="jsDay">
              <el-input v-model="form.jsDay" placeholder="请输入月结算日" />
            </el-form-item>
          </el-col>
          <!--<el-col :span="12">
            <el-form-item label="创建人" prop="createUser">
              <el-input v-model="form.createUser" placeholder="请输入创建人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修改人" prop="updateUser">
              <el-input v-model="form.updateUser" placeholder="请输入修改人" />
            </el-form-item>
          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listJsRules, getJsRules, delJsRules, addJsRules, updateJsRules } from "@/api/business/jsRules";
  import {getToken} from "@/utils/auth";
  export default {
    name: "JsRules",
    dicts: ['jiesuan_type', 'ruku_type', 'jiesuan_contro', 'chuku_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 结算规则表格数据
        jsRulesList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          tenantId: null,
          type: null,
          warehouseCode: null,
          warehouseName: null,
          chukuType: null,
          rukuType: null,
          ckCode: null,
          ckName: null,
          rkCode: null,
          rkName: null,
          supplierCode: null,
          supplierName: null,
          manufacturerCode: null,
          manufacturerName: null,
          tgState: null,
          isJiesuan: null,
          jsZq: null,
          jsDay: null,
          createUser: null,
          updateUser: null,
        },
        upload: {
          // 是否显示弹出层
          open: false,
          // 弹出层标题
          title: "导入`jsRules`信息",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/business/jsRules/importData"
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询结算规则列表 */
      getList() {
        this.loading = true;
        listJsRules(this.queryParams).then(response => {
          this.jsRulesList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          tenantId: null,
          type: null,
          warehouseCode: null,
          warehouseName: null,
          chukuType: null,
          rukuType: null,
          ckCode: null,
          ckName: null,
          rkCode: null,
          rkName: null,
          supplierCode: null,
          supplierName: null,
          manufacturerCode: null,
          manufacturerName: null,
          tgState: null,
          isJiesuan: null,
          jsZq: null,
          jsDay: null,
          createUser: null,
          createTime: null,
          updateUser: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加结算规则";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getJsRules(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改结算规则";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateJsRules(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addJsRules(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除数据？').then(function() {
          return delJsRules(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/jsRules/export', {
          ...this.queryParams
        }, `jsRules_${new Date().getTime()}.xlsx`)
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "结算规则导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download('business/jsRules/importTemplate', {
        }, `结算规则模板.xlsx`)
      },
      /** 文件上传中处理 */
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      /** 文件上传成功处理 */
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      /** 提交上传文件 */
      submitFileForm() {
        this.$refs.upload.submit();
      },
    }
  };
</script>
